import core.*;
import core.util.*;
import util.*;

class util.LudusLoader
{
	private var movieName:String;
	private var location:MovieClip;
	private var listener:Object;
	private var loadByteZero:Boolean = false;
	
	private var pausedLoadCount:Number;
	private var lastBytesLoaded:Number;
	private var intervalStartLoad:Number;
	private var intervalInitStartLoad:Number;
	private var intervalProgress:Number;
	private var intervalLoaded:Number;
	private var intervalLoad :Number;
	private var myMovieClipLoader:MovieClipLoader

	public function LudusLoader()
	{
		myMovieClipLoader = new MovieClipLoader();
		myMovieClipLoader.addListener(this);
	}

	public function loadMovie(movieName:String, location:MovieClip, listener:Object):Void
	{
		this.movieName = movieName;
		this.listener = listener;
		this.location = location;

		//loadMovieByLoad(movieName);
		
		
		myMovieClipLoader.loadClip(movieName, location);
				
		_root.progressBar.bar._xscale = 0;
		_root.progressBar.value.text = "0 %";
		_root.progressBar._visible = true;
	}

	/*private function loadMovieByLoad(movieName:String):Void
	{
		loadByteZero = false;
		pausedLoadCount = 0;
		lastBytesLoaded = 0;
		
		location.loadMovie(movieName);

		//intervalProgress = setInterval(this, "onLoadProgress", 1, this);
		intervalProgress = setInterval(this, "testTotalSize", 100, this);
		
		//intervalInitStartLoad = setInterval(this, "initTestStartMovieClipLoad", 50, this);
		
	}*/
	
	/*public function initTestStartMovieClipLoad():Void
	{
		clearInterval(intervalInitStartLoad);
		intervalStartLoad = setInterval(this, "testStartMovieClipLoad", 10, this);
	}*/

	/*private function testStartMovieClipLoad():Void
	{
		if (location.getBytesTotal() >= 0)
		{
			onLoadStart();
			clearInterval(intervalStartLoad);

			initTestMovieClipLoad();
		}
		else
			Logger.trace("Nao tem tamanho ainda!");
	}*/

	/*public function initTestMovieClipLoad():Void
	{
		intervalLoad = setInterval(this, "testMovieClipLoad", 10, this);
	}*/
	
	/*public function testMovieClipLoad():Void
	{
		if (location.getBytesTotal() == location.getBytesLoaded())
		{
			informMovieLoaded();
			clearInterval(intervalLoad);
		}
	}*/
	
	//Chamado quando os primeiros bytes do arquivo sÃÂ£o carregados
	
	
	public function onLoadInit():Void
	{
		listener.onLoadInit(location);
		_root.progressBar._visible = false;
	}
	
	private function onLoadStart():Void
	{
		listener.onLoadStart(location);
	}

	public function onLoadError():Void
	{
		Logger.trace("Identificou erro na Carga");
		_root.progressBar.value.text = "Erro na Carga";
		intervalProgress = setInterval(this, "reloadClip", 1000, this);	
	}
	
	public function reloadClip():Void
	{
		_root.progressBar.value.text = "Tentando Novamente";
		clearInterval(intervalProgress);
		myMovieClipLoader.loadClip(movieName, location);
	}
		
	public function onLoadProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void
	{
		var progress:Number = 0;
		if (bytesLoaded != 0 && bytesTotal != 0 && bytesLoaded != null && bytesTotal != null)
			progress = Math.floor( bytesLoaded * 100 / bytesTotal );

		listener.onLoadProgress(progress);

		_root.progressBar.bar._xscale = progress;
		_root.progressBar.value.text = progress + " %";
		
	}

	public function toString():String
	{
		return "LudusLoader";
	}
}